import dohttp
import json,time,base64
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36',
}
def gettoken(js:str,command_line_arguments="",stdin="")->str:
    url = "https://runcode.bejson.com/try_run?action=get_token"
    res = dohttp.post(url, headers=header, data={"source_code": js,"language_id":63,"command_line_arguments":command_line_arguments,"stdin":stdin})
    data = json.loads(res.text)
    return data['token']
def getresult(token:str)->str:
    url = f"https://runcode.bejson.com/try_run?action=get_result&token={token}"
    while True:
        res = dohttp.get(url, headers=header)
        data = json.loads(res.text)
        if data['stdout'] != None :
            break
        time.sleep(1)
        
    return base64.b64decode(data['stdout']).decode('utf-8')
def runjs(js:str,command_line_arguments="",stdin="")->str:
    token = gettoken(js,command_line_arguments=command_line_arguments,stdin=stdin)
    result =getresult(token)
    return result

